package miiteen.customsportal.basic.service;

import miiteen.customsportal.basic.beans.BasicOverview;
import miiteen.customsportal.basic.beans.BasicOverviewImage;
import miiteen.customsportal.basic.dao.BasicOverviewDao;
import miiteen.customsportal.basic.dao.BasicOverviewImageDao;
import miiteen.data.dao.BaseDao;
import miiteen.data.service.impl.BaseServiceImpl;
import miiteen.hr.pubconfig.CurrentHrInfo;
import miiteen.hr.user.beans.UserUserInfo;
import miiteen.sys.login.bean.UserShiro;
import miiteen.sys.login.utils.LoginUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Name: CustomsPortal_ Basic_Overview
 * Des: -Service
 * Author: yulp
 * Date: 2020-07-03
 */
@Service(" BasicOverviewService")
public class BasicOverviewService extends BaseServiceImpl<BasicOverview> {

    @Autowired
    private BasicOverviewDao basicOverviewDao;

    @Autowired
    private BasicOverviewImageDao basicOverviewImageDao;

    @Override
    protected BaseDao<BasicOverview> getDao() {
        return basicOverviewDao;
    }

    @Transactional
    public long save(BasicOverview basicOverview){
        UserShiro.ShiroUser shiroUser= LoginUtil.getCurUser();
        UserUserInfo userInfo= CurrentHrInfo.getUserInfo(shiroUser);
        boolean isUpdate=basicOverview.getId()>0;
        if(basicOverview.getId()>0){
            //修改
            basicOverview.setUpdateId(userInfo.getId());//修改人id
            basicOverview.setUpdateTime(new Date());//修改时间
            basicOverview.setUpdator(userInfo.getTrueName());//修改人名
        }else{
            //保存
            basicOverview.setOrganizeId(2001);//组织id
            basicOverview.setCreateId(userInfo.getId());//创建人id
            basicOverview.setCreateTime(new Date());//创建时间
            basicOverview.setCreator(userInfo.getTrueName());//创建人名
        }

        long result=basicOverviewDao.save(basicOverview);

        if(result>0){
//           清空子表数据

            BasicOverviewImage basicOverviewImage=new BasicOverviewImage();
            basicOverviewImage.setDetail(basicOverview.getDetail());
            basicOverviewImage.setOverviewId(basicOverview.getId());
            Map<String,Object> map=new HashMap<>();
            map.put("overviewId",basicOverview.getId());
            List<BasicOverviewImage> list= basicOverviewImageDao.findListByMap(map);
            if(isUpdate){
                if(list.size()>0){
                    basicOverviewImage.setId(list.get(0).getId());
                }
            }
            basicOverviewImageDao.save(basicOverviewImage);
/*
            for(BasicOverviewImage basicOverviewImage:basicOverview.getList()){
                    basicOverviewImage.setOverviewId(basicOverview.getId());
                    if(basicOverviewImageDao.save(basicOverviewImage)<0){
                        throw  new RuntimeException("保存失败");
                    }
            }*/
        }
        return result;
    }

    @Transactional
    public int delete(String ids){
        int result=basicOverviewDao.delete(ids);
        basicOverviewImageDao.delete(ids);
        return result;
    }

}
